package com.wl.DataBase;

import java.sql.*;

/**
 * MySQL 服务类
 * @author TaiZai
 * @data 2024/10/9 16:47
 */
public class MysqlServer {
    private static final String dbUrl = "jdbc:mysql://localhost:3306/aiit_test";
    private static final String dbUser = "root";
    private static final String dbPwd = "wl18256618943@";

    // 建立数据库连接
    public static Connection connect() {
        Connection connection = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");

            // 获取连接
            connection = DriverManager.getConnection(dbUrl, dbUser, dbPwd);
            System.out.println("数据库连接成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }

    // 关闭数据库连接
    public static void closeConnection(Connection connection) {
        try {
            if (connection != null && !connection.isClosed()) {
                connection.close();
                System.out.println("数据库连接已关闭");
            }
        } catch (SQLException e) {
            System.out.println("关闭数据库连接时发生错误: " + e.getMessage());
        }
    }


}
